<?php
require_once 'ApibaseController.php';

/**
 * 法人専用問い合わせAPI<br/>
 *
 * @package apamanshop
 * @author H.Imamura
 * @copyright (c) 2012 SystemSoft Corp. All rights reserved
 */
class Inquiry_ApiasmemberetcController extends Inquiry_ApibaseController
{
	/**
	 * メインアクション
	 *
	 * @access public
	 * @return void
	 */
	public function indexAction()
	{
		// 変数初期化
		$api_params = array();
		$api_params['ComId']	= $this->_params['ComId'];		// 企業ID
		$api_params['Name']		= $this->_params['Name'];		// 名前（姓・名）
		$api_params['NameSei']	= $this->_params['NameSei'];	// 名前（姓）
		$api_params['NameMei']	= $this->_params['NameMei'];	// 名前（名）
		$api_params['AgeKbn']	= $this->_params['AgeKbn'];		// 年齢（区分）
		$api_params['AgeNm']	= $this->_params['AgeNm'];		// 年齢
		$api_params['Sex']		= $this->_params['Sex'];		// 性別（区分）
		$api_params['SexNm']	= $this->_params['SexNm'];		// 性別
		$api_params['Tel1']		= $this->_params['Tel1'];		// TEL1
		$api_params['Tel2']		= $this->_params['Tel2'];		// TEL2
		$api_params['Tel3']		= $this->_params['Tel3'];		// TEL3
		$api_params['Tel']		= $this->_params['Tel'];		// TEL(ALL)
		$api_params['Fax1']		= $this->_params['Fax1'];		// FAX1
		$api_params['Fax2']		= $this->_params['Fax2'];		// FAX2
		$api_params['Fax3']		= $this->_params['Fax3'];		// FAX3
		$api_params['Fax']		= $this->_params['Fax'];		// FAX(ALL)
		$api_params['Mail']		= $this->_params['Mail'];		// MAIL
		$api_params['Comment']	= $this->_params['Comment'];	// お問い合わせ内容
		$api_params['TimeStr']	= $this->_params['TimeStr'];	// 送信時刻
		
		// 企業情報取得
		$this->_model = new Inquiry_Model_AsnEntry();
		$company = $this->_model->getCompany($this->_params['ComId']);
		$api_params['ComName']	= $company['M86_COMPANY_NAME'];
		$api_params['GenruFlg']	= $company['M86_GENRU_FLAG'];
		$api_params['EtcMail']	= $company['M86_ETC_MAIL'];
		
		//--- メール判定 ---
		$this->setSendMailFlag($this->_params['Mail']);
		
		try {
			// ﾒｰﾙ送信処理 ********************************************
			$result =  $this->SendMailMain($api_params);

			if($result == "0"){
				 $this->raiseError('メール送信エラー');
			}

			$result = 1;
		} catch (Exception $e) {
			__log_error(__FILE__, __LINE__, __METHOD__, "メール送信エラー");
			$result = 0;
		}

		// 返却データ作成
		$resResult = array('api_result' => array('result' => $result, 'res_detail' => ''));
		// 返却処理
		$this->_api_data = $resResult;
		$this->display();
	}

	//メール送信制御
	private function SendMailMain($addinfo) {
		
		// 企業メール送信
		if (empty($addinfo['EtcMail'])) {
			// 企業メールアドレスが無い場合はエラー
			return '0';
		}
		$mailResult = $this->SendMailCompany($addinfo);

		// ユーザへメール送信
		if (!empty($addinfo['Mail']) && $mailResult) {
			$mailResult = $this->SendMailUser($addinfo);
		}

		$result = "1";
		if (!$mailResult) {
			$result = "0";
		}
		return $result;
	}

	// 問い合わせユーザへのメール送信
	private function SendMailUser($addinfo) {

		// from(送信元)、to（あて先)
		$strMailFrom = array(Apm_Config_Mail::MAIL_AS => "");		//送信元ﾒｰﾙｱﾄﾞﾚｽ
		$strMailTo = array($addinfo['Mail'] => "");					//送信先ﾒｰﾙｱﾄﾞﾚｽ

		// メール本文に必要なパラメータ
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_SUBJECT_KEY] = "［アパマンショップ］お問い合わせのご確認(" . $addinfo['ComName'] . ")";
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_FROM_KEY] = $strMailFrom;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = $strMailTo;

		return $this->sendToiawaseMailJudge("MailAsmemberEtcUser.tpl", $addinfo, $mail_params, 3);
	}

	// 企業メール送信
	private function SendMailCompany($addinfo) {

		$strMailFrom = array(Apm_Config_Mail::MAIL_AS => "");			//送信元ﾒｰﾙｱﾄﾞﾚｽ
		$strMailCc = array(Apm_Config_Mail::MAIL_AS => "", "morishige@apamanshop.co.jp" => "");
		//$strMailBcc = array(Apm_Config_Mail::MAIL_NETWORK => "");

		//$strMailTo = array($addinfo['EtcMail'] => "");
		$arrMailTo = explode(",", $addinfo['EtcMail']);
		foreach ($arrMailTo as $val) {
			$strMailTo[$val] = "";
		}

		//@ 送信先ﾒｰﾙｱﾄﾞﾚｽ
		// 本文の設定
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_SUBJECT_KEY] = "［アパマンショップ］" . $addinfo['ComName'] . "専用ページからのお問い合わせ";
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_FROM_KEY] = $strMailFrom;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = $strMailTo;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_CC_KEY] = $strMailCc;
		//$mail_params[Apm_Otheritems::SEND_MAIL_EX_BCC_KEY] = $strMailBcc;

		return $this->sendToiawaseMailJudge("MailAsmemberEtc".$addinfo['ComId'].".tpl", $addinfo, $mail_params, 2);
	}

}
?>
